import java.util.Arrays;

public class DoubleIndex {
    public static void main(String[] args) {
//        boolean a = happyCount(100);
//        System.out.println(a);
        //int[] arr = {10,9,8,7,6,5,4,3,2,1};
        //int[] arr2 = {2,2,3,4,6,9};
        //int size = check(arr2);
        //System.out.println(size);
        //doubleIndex3(arr);
        //repeat(arr);
        //int result = fullWater(arr);
        //System.out.println(result);
//        for (int x:arr) {
//            System.out.print(x + " ");
//         }
        int[] arr1 ={3,2,2,3};
        int num = removeElement(arr1 , 3);
        System.out.println(num);

    }
    //移动零
    public static void doubleIndex(int[] arr){
        int dest = -1;
        int cur = 0;
        for(int i = 0 ;i< arr.length ;i++) {
            if(arr[i] == 0) {
                dest = i - 1;
                break;
            }
        }
        while(cur < arr.length) {
            while(arr[cur] == 0 && (cur + 1) < arr.length && arr[cur + 1] != 0){
                cur++;
                if(cur < arr.length){
                swap(arr,dest + 1,cur);
                }else {
                    break;
                }
                dest++;
            }
            cur++;
        }

    }
    public static void swap(int[] arr , int i , int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }
    //进阶版
    public static void doubleIndex2(int[] arr){
        for(int cur = 0,dest = -1 ; cur < arr.length; cur++) {
            if (arr[cur] != 0) {
                dest++;
                swap(arr,cur,dest);
            }
        }

    }
    public static void doubleIndex3(int[] arr){
        for(int cur = 0, dest = -1 ; cur < arr.length; cur++) {
            if(arr[cur] != 0) {
                dest++;
                if(cur != dest) {
                    swap(arr , cur ,dest);
                }
            }
        }
    }
    //复写0
    public static void repeat(int arr[]){
        int count = 0;
        for(int i = 0; i < arr.length; i++){
            if(arr[i] == 0) {
                count++;
            }
        }
        int cur = arr.length - 1;
        while(count > 0) {
            cur--;
            count--;
        }
        int desc = arr.length - 1;
        while(cur >= 0) {
            if(arr[cur] == 0) {
                arr[desc--] = 0;
                if(desc >= 0) {
                    arr[desc--] = 0;
                }

            }else{
                arr[desc] = arr[cur];
                desc--;
            }
            cur--;
        }
    }
    //快乐数
    public static boolean happyCount(int count){
        int left = count;
        int right = result(count);
        while( left != right ) {
          left =  result(left);
          right = result(right);
          right = result(right);
        }
        if(left == 1){
        return true;
        }
        else {
            return false;
        }
    }



    public static int result(int count){
        int sum = 0;
        while(count > 0) {
            int i = count % 10;
            sum = i * i + sum;
            count = count / 10;
        }
        return  sum;
    }
    //最大容积去装水
    public static  int fullWater(int[] arr){
        int left = 0;
        int right = arr.length - 1 ;
        int max = 0;
        while(left < right){
             if(max < volume(arr,left,right)){
                 max = volume(arr , left ,right);
             }
            if(arr[left] < arr[right]) {
                int tmp = arr[left];
                left++;

                while(left < right && arr[left] < tmp) {
                    left++;
                }
            }
            else {
                int tmp = arr[right];
                right--;
                while (left < right && arr[right] < tmp) {
                    right--;
                }
            }
        }
        return max;
    }
    public static int volume(int[] arr , int left , int right){
        if(arr[left] < arr[right]){
        return arr[left] * (right - left );
        }else {
            return arr[right] * (right - left );
        }
    }
    //有效三角形个数
    public static  int check (int[] arr){
        //quickSort(arr);
        Arrays.sort(arr);
        int cur = arr.length - 1;
        int total = 0;
        int sum = 0;

        while(cur > 1) {

            int left = 0;

            while(left < cur - 1) {
                int right = cur - 1;

                while(right > left && arr[left] + arr[right] > arr[cur]) {
                    right--;
                }
                sum = cur - right - 1;
                total = total + sum;
                left++;
            }
            cur--;

        }
        return total;
    }
    //算法优化
    //时间复杂度：O(n2);
    //空间：O（1）；
    public int triangleNumber(int[] arr) {
        Arrays.sort(arr);
        int total = 0;
        int cur = arr.length - 1;
        while(cur > 1) {
            int left = 0;
            int right = cur - 1;
            while(left < right) {
                if(left < right && arr[left] + arr[right] > arr[cur]){
                    total = total + right - left;
                    right--;

                }else{
                    left++;
                }
            }
            cur--;
        }
        return total;
    }

    //public static void quickSort(int[] arr , int left ){}
    //求和为s的数：
    public int[] twoSum(int[] price, int target) {
        int start = 0;
        int end = price.length - 1;
        int[] arr = new int[2];

        while (start < end) {
            if (price[start] + price[end] > target) {
                end--;
            } else if (price[start] + price[end] < target) {
                start++;
            }
            else {
                arr[0] = price[start];
                arr[1] = price[end];
                break;
            }
        }
        return arr;
    }
    //数组划分，移除全部等于val的元素，并返回剩下元素的个数
    public static int removeElement(int[] nums, int val) {
        int desc = -1;
        int cur = 0;
        while(cur < nums.length) {
            if(nums[cur] != val) {
                desc++;
                if(cur != desc){
                    int tmp = nums[desc];
                    nums[desc] = nums[cur];
                    nums[cur] = tmp;
                }
                cur++;
            }else{
                cur++;
            }
        }
        System.out.println(desc + 1);
        return desc + 1;


    }


}

